import {
	getInfo
} from "../../api/user.js";
import {
	LOGIN_STATUS,
	UID
} from '../../config/cache';
import Cache from '../../utils/cache';
import {
	USER_INFO
} from '../../config/cache';

const state = {
	token: Cache.get(LOGIN_STATUS) || null,
	backgroundColor: "#fff",
	userInfo: null,
	uid: Cache.get(UID) || null,
	homeActive: false,
	enableIntegral: {},
	iconList: [{
			name: "大陆",
			site: 'cn',
			id: 1
		},
		{
			name: "港澳",
			site: 'hk',
			id: 2
		},
		{
			name: "欧洲",
			site: 'ouzhou',
			id: 3
		},
		{
			name: "保税区",
			site: 'baoshui',
			id: 4
		},
		{
			name: "法国",
			site: 'faguo',
			id: 5
		},
		{
			name: "韩国",
			site: 'hanguo',
			id: 6
		},
		{
			name: "澳洲",
			site: 'aozhou',
			id: 7
		},
		{
			name: "德国",
			site: 'deguo',
			id: 8
		},
		{
			name: "日本",
			site: 'riben',
			id: 9
		},
		{
			name: "美国",
			site: 'meiguo',
			id: 10
		},
		{
			name: "意大利",
			site: 'Italy',
			id: 11
		},
	],
	statusMap: [{
			name: '待付款',
			value: 0
		},
		{
			name: '待发货',
			value: 1
		},
		{
			name: '待收货',
			value: 2
		},
		{
			name: '待评价',
			value: 3
		},
		{
			name: '已完成',
			value: 4
		},
		{
			name: '已取消',
			value: 5
		},
		{
			name: '待确认',
			value: 6
		},
		{
			name: '退款中',
			value: -1
		},
		{
			name: '退货退款',
			value: -2
		},
		{
			name: '补偿退款',
			value: -3
		}
	],
	name: '',
	avatar: '',
	merId: '',
	merName: '',
	appletType: '',
	autoPush:0
};

const mutations = {
	LOGIN(state, opt) {
		state.token = opt.token;
		Cache.set(LOGIN_STATUS, opt.token, opt.time);
	},
	SETUID(state, val) {
		state.uid = val;
		Cache.set(UID, val);
	},
	UPDATE_LOGIN(state, token) {
		state.token = token;
	},
	LOGOUT(state) {
		state.token = undefined;
		state.uid = undefined
		Cache.clear(LOGIN_STATUS);
		Cache.clear(UID);
	},
	BACKGROUND_COLOR(state, color) {
		state.color = color;
		document.body.style.backgroundColor = color;
	},
	UPDATE_USERINFO(state, userInfo) {
		state.userInfo = userInfo;
	},
	OPEN_HOME(state) {
		state.homeActive = true;
	},
	CLOSE_HOME(state) {
		state.homeActive = false;
	},
	USER_ENABLEINTEGRAL(state, integral) {
		state.enableIntegral = integral;
	},

	SET_NAME: (state, name) => {
		state.name = name
	},
	SET_AVATAR: (state, avatar) => {
		state.avatar = avatar
	},
	SET_MERID: (state, merId) => {
		state.merId = merId
	},
	SET_MERNAME: (state, merName) => {
		state.merName = merName
	},
	SET_APPLETTYPE: (state, appletType) => {
		state.appletType = appletType
	},
	SET_SECONDHAND: (state, secondHand) => {
	    state.secondHand = secondHand
	},
	SET_AUTOPUSH: (state, autoPush) => {
	    state.autoPush = autoPush
	},
};

const actions = {
	getInfo({
		commit,
		state
	}) {
		return new Promise((resolve, reject) => {
			getInfo(state.token).then(data => {
				console.log(data, 'getInfo');
				if (!data) {
					reject('Verification failed, please Login again.')
				}
				const {
					account,
					merId,
					merName,
					appletType,
					secondHand,
					autoPush
				} = data.data
				let merIdArr = JSON.parse(merId);
				if (merIdArr.length > 0) {
					merIdArr = merIdArr[0]
				} else {
					merIdArr = null
				}
				commit('SET_NAME', account)
				commit('SET_AVATAR',
					'https://italytao.oss-cn-shanghai.aliyuncs.com/test-admin-image/73ac73d47c764c3c09a33700w8puq5bwo1.png'
				)
				commit('SET_MERID', merIdArr ? merIdArr : 0)
				commit('SET_MERNAME', merName)
				commit('SET_SECONDHAND', secondHand)
				commit('SET_APPLETTYPE', appletType)
				commit('SET_AUTOPUSH', autoPush)
				console.info(autoPush,123)
				resolve(data.data)
			}).catch(error => {
				reject(error)
			})
		})
	},
};

export default {
	state,
	mutations,
	actions
};
